package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.OperatorService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.common.util.code.MD5;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.OperatorDao;
import com.qdlc.p2p.dal.dao.OperatorRoleDao;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.OperatorRole;
import com.qdlc.p2p.dal.dto.Role;

@Service("managerService")
public class OperatorServiceImpl implements OperatorService {

    @Resource
    private OperatorDao operatorDao;

    @Resource
    private OperatorRoleDao operatorRoleDao;

    public Operator login(String userName, String pwd) throws Exception {
        if (userName == null || pwd == null) {
            throw new BusinessException("用户名和密码不能为空！", 1);
        }
        Operator operator = operatorDao.findObjByProperty("userName", userName);
        if (operator != null && MD5.encode(pwd).equals(operator.getPwd()) && operator.getStatus() == 0) {
            operator.setLoginIp(Global.getIP());
            operator.setLoginTime(DateUtil.getNow());
            return operator;
        } else if (operator != null && operator.getStatus() != 0) {
            throw new BusinessException("账号已被禁用！", 1);
        }
        throw new BusinessException("用户名或密码错误！", 1);
    }

    public void addOperator(Operator operator, String[] roleIdArr) {
        int hasName = operatorDao.countByName(operator.getName());
        if (hasName > 1) {
            throw new BusinessException("用户名已被使用！", 1);
        }
        operator.setAddTime(DateUtil.getNow());
        operator.setUpdateTime(DateUtil.getNow());
        operator.setPwd(MD5.encode(operator.getPwd()));
        operator = operatorDao.save(operator);
        for (int i = 0; i < roleIdArr.length; i++) {
            String roleIdStr = roleIdArr[i];
            long roleId = Long.parseLong(roleIdStr);
            Role r = new Role(roleId);
            OperatorRole operatorRole = new OperatorRole();
            operatorRole.setOperator(operator);
            operatorRole.setRole(r);
            operatorRoleDao.save(operatorRole);
        }
    }

    public void userUpdate(Operator operator, String[] roleIdArr) {
        operator.setUpdateTime(DateUtil.getNow());
        Operator u = operatorDao.find(operator.getId());
        operator.setPwd(u.getPwd());
        operatorDao.update(operator);
        operatorRoleDao.deleteByUserId(u.getId());
        for (int i = 0; i < roleIdArr.length; i++) {
            String roleIdStr = roleIdArr[i];
            long roleId = Long.parseLong(roleIdStr);
            Role r = new Role(roleId);
            OperatorRole operatorRole = new OperatorRole();
            operatorRole.setOperator(operator);
            operatorRole.setRole(r);
            operatorRoleDao.save(operatorRole);
        }
    }

    public Operator getUserById(long id) {
        return operatorDao.userFind(id);
    }

    public PageDataList<Operator> getUserPageList(int pageNumber, int pageSize, String searchName, Operator model) {
        QueryParam param = QueryParam.getInstance().addPage(pageNumber, pageSize);
        String name = model.getName();
        String loginName = model.getUserName();
        if (!StringUtil.isBlank(searchName)) {//模糊查询条件
            SearchFilter orFilter1 = new SearchFilter("userName", Operators.LIKE, searchName.trim());
            SearchFilter orFilter2 = new SearchFilter("name", Operators.LIKE, searchName.trim());
            param.addOrFilter(orFilter1, orFilter2);
        } else { //精确查询条件
            if (StringUtil.isNotBlank(name)) {
                param.addParam("name", Operators.LIKE, name);
            }
            if (StringUtil.isNotBlank(loginName)) {
                param.addParam("userName", Operators.LIKE, loginName);
            }
        }
        param.addOrder(OrderType.DESC, "id");
        return operatorDao.findPageList(param);
    }

    public void userUpdate(Operator operator) {
        operatorDao.update(operator);
    }

    public void updateUserPwd(Operator operator) {
        operator.setUpdateTime(DateUtil.getNow());
        this.userUpdate(operator);
    }

    public Operator getUserByUserName(String userName) {
        if (userName == null || userName.length() <= 0) {
            return null;
        }
        return operatorDao.getManagerByUserName(userName);
    }

    @Override
    public Operator findById(long id) {
        return operatorDao.find(id);
    }

    @Override
    public void updateStatus(long id, int status) throws Exception {
        operatorDao.updateStatus(id, status);
    }
}
